﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinCalcManager;
using Infragistics.Win.CalcEngine;
using System.Collections;

namespace BYManageInfoSystem
{
    public partial class Frm_RecvFacFee_New2 : DockContent
    {

        private DGBY.BLL.FacFeeTabNew m_bllFacFeeTabNew = new DGBY.BLL.FacFeeTabNew();
        private bool m_bInsertFlag = false;
        private int m_nDelIndex = -1;
        public Frm_RecvFacFee_New2()
        {
            InitializeComponent();
        }
  
        private void Frm_RecvFacFee_New2_Load(object sender, EventArgs e)
        {
            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
        }
        private void ShowAllRecord(string strWhere)
        {
       
            int nYear = this.dateTimePicker1.Value.Date.Year;
            int nMonth = this.dateTimePicker1.Value.Date.Month;
            int nYearMonth = 0;
            nYearMonth = nYear * 100 + nMonth;
            DataSet ds = m_bllFacFeeTabNew.GetInfoListEx(strWhere,nYearMonth);
            ultraGrid1.DataSource = null;

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            //ultraGrid1.DisplayLayout.Bands[0].Columns[1].Hidden = true;

          
        }
        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }
        public static void ultraGrid1_InitializeRowFilter(Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            //e.Layout.Override.RowFilterAction = RowFilterAction.DisableFilteredOutRows;
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;
            //e.Layout.Override.RowFilterMode = RowFilterMode.AllRowsInBand;
            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.

            e.Layout.Override.FilterOperandStyle = FilterOperandStyle.DropDownList;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);

            //   UltraGridBand band = e.Layout.Bands[0];
            //   band.ColumnFilters["原币金额"].FilterConditions.Clear();
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.GreaterThan, 5);
            //   band.ColumnFilters["原币金额"].FilterConditions.Add(

            //FilterComparisionOperator.LessThan, 10);
            //   band.ColumnFilters["原币金额"].LogicalOperator = FilterLogicalOperator.And;
        }
        	
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            if (this.checkBox1.Checked == true)
                ultraGrid1_InitializeRowFilter(e);
            else
                ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            SummarySettings averageSummary = null;
            AddAverageSummaryColumns(averageSummary, e, "应收合计");
            AddAverageSummaryColumns(averageSummary, e, "应付合计");
            AddAverageSummaryColumns(averageSummary, e, "商检应收");
            AddAverageSummaryColumns(averageSummary, e, "商检应付");
            AddAverageSummaryColumns(averageSummary, e, "报关应收合计");
            AddAverageSummaryColumns(averageSummary, e, "报关应付合计");
            AddAverageSummaryColumns(averageSummary, e, "应收通关单费");
            AddAverageSummaryColumns(averageSummary, e, "应收报关费");
            AddAverageSummaryColumns(averageSummary, e, "应收港口建设费");
            AddAverageSummaryColumns(averageSummary, e, "应收连柜费");
            AddAverageSummaryColumns(averageSummary, e, "应收加页费");
            AddAverageSummaryColumns(averageSummary, e, "应收GPS费");
            AddAverageSummaryColumns(averageSummary, e, "应收查柜费");
            AddAverageSummaryColumns(averageSummary, e, "应收改船费");
            AddAverageSummaryColumns(averageSummary, e, "应收扫描费");
            AddAverageSummaryColumns(averageSummary, e, "应收茶水费");
            AddAverageSummaryColumns(averageSummary, e, "报关应收其他费用");
            AddAverageSummaryColumns(averageSummary, e, "大陆船务应收");
            AddAverageSummaryColumns(averageSummary, e, "大陆船务应付");
            AddAverageSummaryColumns(averageSummary, e, "香港船务应收");
            AddAverageSummaryColumns(averageSummary, e, "香港船务应付");
            AddAverageSummaryColumns(averageSummary, e, "国外快递应收");
            AddAverageSummaryColumns(averageSummary, e, "国外快递应付");
            AddAverageSummaryColumns(averageSummary, e, "单证应收");
            AddAverageSummaryColumns(averageSummary, e, "单证应付");
            AddAverageSummaryColumns(averageSummary, e, "检测费应收");
            AddAverageSummaryColumns(averageSummary, e, "检测费应付");
            AddAverageSummaryColumns(averageSummary, e, "纸箱证应收");
            AddAverageSummaryColumns(averageSummary, e, "纸箱证应付");
        }

        private void AddAverageSummaryColumns(SummarySettings averageSummary, InitializeLayoutEventArgs e, string strColumns)
        {
            averageSummary = e.Layout.Bands[0].Summaries.Add(strColumns, SummaryType.Sum, e.Layout.Bands[0].Columns[strColumns]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;     

                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;
                        //if (strTemp == "经营公司" | strTemp == "客户" | strTemp == "币种" | strTemp == "收款方式")
                        //    return;
                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nFacFeeTabNewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                DGBY.Model.FacFeeTabNew modelFacFeeTabNew
                    = m_bllFacFeeTabNew.GetModel(nFacFeeTabNewID);

                modelFacFeeTabNew.SJJinE = Convert.ToDecimal(CurrentUGR.Cells["商检金额"].Value.ToString());
                modelFacFeeTabNew.ShouldRecvSJFee = Convert.ToDecimal(CurrentUGR.Cells["应收商检费"].Value.ToString());
                modelFacFeeTabNew.ShouldRecvOtherSJFee = Convert.ToDecimal(CurrentUGR.Cells["应收商检其它费用"].Value.ToString());
                
                modelFacFeeTabNew.YSYTFee = Convert.ToDecimal(CurrentUGR.Cells["异提费"].Value.ToString());
                modelFacFeeTabNew.YSDDFee = Convert.ToDecimal(CurrentUGR.Cells["打单费"].Value.ToString());
                modelFacFeeTabNew.YSCZFee = Convert.ToDecimal(CurrentUGR.Cells["超重费"].Value.ToString());
                modelFacFeeTabNew.YSYCFee = Convert.ToDecimal(CurrentUGR.Cells["押车费"].Value.ToString());
                modelFacFeeTabNew.YSGSFee = Convert.ToDecimal(CurrentUGR.Cells["高速费"].Value.ToString());
                modelFacFeeTabNew.YSGBFee = Convert.ToDecimal(CurrentUGR.Cells["过磅费"].Value.ToString());
                modelFacFeeTabNew.YSPGFee = Convert.ToDecimal(CurrentUGR.Cells["拼柜费"].Value.ToString());
                modelFacFeeTabNew.YSDCFee = Convert.ToDecimal(CurrentUGR.Cells["堆存费"].Value.ToString());
                modelFacFeeTabNew.YSDGFee = Convert.ToDecimal(CurrentUGR.Cells["吊柜费"].Value.ToString());
                modelFacFeeTabNew.YSFTFee = Convert.ToDecimal(CurrentUGR.Cells["封条费"].Value.ToString());
                modelFacFeeTabNew.YSFKFee = Convert.ToDecimal(CurrentUGR.Cells["返空费"].Value.ToString());
                modelFacFeeTabNew.YSGDFee = Convert.ToDecimal(CurrentUGR.Cells["改单费"].Value.ToString());
                modelFacFeeTabNew.YSQTFee = Convert.ToDecimal(CurrentUGR.Cells["运输其他费用"].Value.ToString());
               
                
                modelFacFeeTabNew.BGTGDFee = Convert.ToDecimal(CurrentUGR.Cells["通关单费"].Value.ToString());
                modelFacFeeTabNew.BGBGFee = Convert.ToDecimal(CurrentUGR.Cells["报关费"].Value.ToString());
                modelFacFeeTabNew.BGGKJSFee = Convert.ToDecimal(CurrentUGR.Cells["港口建设费"].Value.ToString());
                modelFacFeeTabNew.BGKAFee = Convert.ToDecimal(CurrentUGR.Cells["口岸费"].Value.ToString());
                modelFacFeeTabNew.BGLGFee = Convert.ToDecimal(CurrentUGR.Cells["连柜费"].Value.ToString());
                modelFacFeeTabNew.BGJYFee = Convert.ToDecimal(CurrentUGR.Cells["加页费"].Value.ToString());
                modelFacFeeTabNew.BGCGFee = Convert.ToDecimal(CurrentUGR.Cells["查柜费"].Value.ToString());
                modelFacFeeTabNew.BGGCFee = Convert.ToDecimal(CurrentUGR.Cells["改船费"].Value.ToString());
                modelFacFeeTabNew.BGSMFee = Convert.ToDecimal(CurrentUGR.Cells["扫描费"].Value.ToString());
                modelFacFeeTabNew.BGCSFee = Convert.ToDecimal(CurrentUGR.Cells["茶水费"].Value.ToString());
                modelFacFeeTabNew.BGOtherFee = Convert.ToDecimal(CurrentUGR.Cells["报关其它费用"].Value.ToString());
                
                     
                modelFacFeeTabNew.ShipDLTiDanNo = CurrentUGR.Cells["大陆船务费提单号"].Value.ToString();
                modelFacFeeTabNew.ShipDLRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收大陆船务费"].Value.ToString());
               
                modelFacFeeTabNew.ShipHKTiDanNo = CurrentUGR.Cells["香港船务费提单号"].Value.ToString();
                modelFacFeeTabNew.ShipHKRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收香港船务费"].Value.ToString());
               

                modelFacFeeTabNew.TransHKRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收香港运输费"].Value.ToString());
                modelFacFeeTabNew.ShouldRecvDHLFee = Convert.ToDecimal(CurrentUGR.Cells["应收国外快递费"].Value.ToString());
                modelFacFeeTabNew.DZShouldRecvMoney = Convert.ToDecimal(CurrentUGR.Cells["应收单证费"].Value.ToString());
                modelFacFeeTabNew.ShouldRecvZXZFee = Convert.ToDecimal(CurrentUGR.Cells["应收纸箱证费"].Value.ToString());
                modelFacFeeTabNew.ShouldRecvJCFee = Convert.ToDecimal(CurrentUGR.Cells["应收检测费"].Value.ToString());
                
                     

                m_bllFacFeeTabNew.Update(modelFacFeeTabNew);


                CurrentUGR.Cells["应收商检费用合计"].Value = modelFacFeeTabNew.ShouldRecvSJFee + modelFacFeeTabNew.ShouldRecvOtherSJFee;

                CurrentUGR.Cells["应收运输费用合计"].Value = modelFacFeeTabNew.YSYTFee
                    + modelFacFeeTabNew.YSDDFee
                    + modelFacFeeTabNew.YSCZFee
                    + modelFacFeeTabNew.YSYCFee
                    + modelFacFeeTabNew.YSGSFee
                    + modelFacFeeTabNew.YSGBFee
                    + modelFacFeeTabNew.YSPGFee
                    + modelFacFeeTabNew.YSDCFee
                    + modelFacFeeTabNew.YSDGFee
                    + modelFacFeeTabNew.YSFTFee
                    + modelFacFeeTabNew.YSFKFee
                    + modelFacFeeTabNew.YSGDFee
                    + modelFacFeeTabNew.YSQTFee;

                CurrentUGR.Cells["应收报关费用合计"].Value = modelFacFeeTabNew.BGTGDFee
               + modelFacFeeTabNew.BGBGFee
               + modelFacFeeTabNew.BGGKJSFee
               + modelFacFeeTabNew.BGKAFee
               + modelFacFeeTabNew.BGLGFee
               + modelFacFeeTabNew.BGJYFee
               + modelFacFeeTabNew.BGCGFee
               + modelFacFeeTabNew.BGGCFee
               + modelFacFeeTabNew.BGSMFee
               + modelFacFeeTabNew.BGCSFee
               + modelFacFeeTabNew.BGOtherFee;

            
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;

                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }


        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }


    }
}
